package com.usefullc.download.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.exception.BizException;
import com.usefullc.common.util.NetworkUtils;
import com.usefullc.download.domain.Video;
import com.usefullc.download.param.CheckRegisterCodeParam;
import com.usefullc.download.param.VideoAnalyseParam;
import com.usefullc.download.service.RegisterCodeService;
import com.usefullc.download.service.VideoService;
import com.usefullc.download.vo.PayInfoVo;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: Sampson
 * @date: 2022-01-10 15:28
 */
@RestController
@RequestMapping(path = "/download/video/")
@Api( tags="视频管理")
public class VideoController extends BaseController {

    @Value("${usefullc.api.host}")
    private String apiHost;

    @Autowired
    private VideoService videoService;

    @Autowired
    private RegisterCodeService registerCodeService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "视频分析", response = WebResult.class)
    @PostMapping(path = "analyse", produces = "application/json")
    public WebResult analyse(@RequestBody VideoAnalyseParam param) {
//        try {
//            String videoCotnent = FileUtils.readFileToString(new File("/Users/long3/Documents/video.html" ),"utf-8");
//            param.setVideoContent(videoCotnent);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        if (StringUtils.isEmpty(param.getMachineId())) {
            throw new BizException("机器码不能为空");
        }
        if (StringUtils.isEmpty(param.getVideoContent())) {
            throw new BizException("视频html不能为空");
        }
        Video video = videoService.analyse(param);
        return WebResultBuilder.successResult(video);
    }



    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取支付信息", response = WebResult.class)
    @PostMapping(path = "getPayInfo", produces = "application/json")
    public WebResult getPayInfo(@RequestBody CheckRegisterCodeParam param, HttpServletRequest request) {
        if (StringUtils.isEmpty(param.getMachineId())) {
            throw new BizException("机器码不能为空");
        }
        String ip = NetworkUtils.getIpAddress(request);
        logger.info("{} 进入支付页面,ip={}",param.getMachineId(),ip);
        PayInfoVo payInfoVo = new PayInfoVo();
        payInfoVo.setPayUrl(apiHost+"/tpl/pay/index.html");
        payInfoVo.setDescription("<p>如果您觉得软件好用，请购买注册码支持下作者，只需要您一顿饭的钱；</p>\n" +
                "            <p>如果您觉的软件还不够资格，可以暂时放弃支持，我们会持续努力打磨好软件；诚惶诚恐，只为了您最大的便捷</p>\n" +
                "            <p>您有任何问题可以加下面微信咨询联系。</p>\n" +
                "            <p><img style=\"width:120px;height:120px\" src=\""+apiHost+"/system/common/res/download/wx.png\"></p>");
        return WebResultBuilder.successResult(payInfoVo);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "暂不支付", response = WebResult.class)
    @PostMapping(path = "payLater", produces = "application/json")
    public WebResult payLater(@RequestBody CheckRegisterCodeParam param, HttpServletRequest request) {
        if (StringUtils.isEmpty(param.getMachineId())) {
            throw new BizException("机器码不能为空");
        }
        String ip = NetworkUtils.getIpAddress(request);
        logger.info("{} 进入支付页面,ip={}",param.getMachineId(),ip);
        registerCodeService.delayProbation(param.getMachineId());
        return WebResultBuilder.successResult();
    }


}
